/// @tags: GCD SegmentTree
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 2e5 + 2;

int A[maxn], B[maxn], difA[maxn], difB[maxn];

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

struct SegmentTree {
  int* base;
  int val[maxn * 4];

  SegmentTree(int* base) : base(base) {}
  void build(int u, int l, int r) {
    if (l == r) {
      val[u] = base[l];
      return;
    }
    int mid = (l + r) >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    val[u] = gcd(val[u << 1], val[u << 1 | 1]);
  }

  int query(int u, int l, int r, int L, int R) {
    if (L <= l && r <= R) return val[u];
    int mid = (l + r) >> 1, ans = 0;
    if (L <= mid) ans = query(u << 1, l, mid, L, R);
    if (mid < R) ans = gcd(ans, query(u << 1 | 1, mid + 1, r, L, R));
    return ans;
  }
} segA(difA), segB(difB);

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> A[i];
  for (int i = 1; i <= n; i++) cin >> B[i];
  for (int i = 1; i <= n; i++) difA[i] = abs(A[i] - A[i - 1]);
  for (int i = 1; i <= n; i++) difB[i] = abs(B[i] - B[i - 1]);
  segA.build(1, 1, n), segB.build(1, 1, n);
  while (q--) {
    int h1, h2, w1, w2;
    cin >> h1 >> h2 >> w1 >> w2;
    int ans = A[h1] + B[w1];
    if (h1 != h2) ans = gcd(ans, segA.query(1, 1, n, h1 + 1, h2));
    if (w1 != w2) ans = gcd(ans, segB.query(1, 1, n, w1 + 1, w2));
    cout << ans << endl;
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("ABC254F Rectangle GCD.in", "r", stdin);
  freopen("ABC254F Rectangle GCD.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
